package com.sz.lk.wingsystem.mvp.model;

import com.google.gson.Gson;
import com.sz.lk.wingsystem.bean.ManualPamars;
import com.sz.lk.wingsystem.bean.ManualResult;
import com.sz.lk.wingsystem.bean.WeightPamars;
import com.sz.lk.wingsystem.bean.WeightResult;
import com.sz.lk.wingsystem.mvp.BaseRequest;
import com.sz.lk.wingsystem.mvp.OnNetRequestListener;
import com.sz.lk.wingsystem.mvp.RxPresenter;
import com.sz.lk.wingsystem.mvp.contract.WeighContract;
import com.sz.lk.wingsystem.mvp.rxuntils.RxScheduler;
import com.sz.lk.wingsystem.mvp.rxuntils.Subscribe2Help;

import io.reactivex.Observable;
import okhttp3.RequestBody;

/**
 * author : liuke
 * e-mail : 1012459343@qq.com
 * date   : 2020/12/6 17:12
 * desc   :
 * version: 1.0
 */
public class WeighModel extends BaseRequest implements WeighContract.Model {
    public WeighModel(RxPresenter mRxPresenter) {
        super(mRxPresenter);
    }

    @Override
    public void WeighCartonNo(String weightStr, OnNetRequestListener<WeightResult> listener) {
        //如果是异步操作就把返回的Disposable 取出 添加
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), new Gson().toJson(new WeightPamars(weightStr)));
        Observable<WeightResult> observable = webRequest.WeighCartonNo(requestBody);
        listener.onStart();
        observable.compose(RxScheduler.<WeightResult>Obs_io_main())

                .subscribe(new Subscribe2Help<WeightResult>(mRxPresenter) {
                    @Override
                    public void onNext(WeightResult value) {
                        listener.onSuccess(value);
                    }

                    @Override
                    public void onError(Throwable throwable) {
                        super.onError(throwable);
                        listener.onFailure(throwable);
                    }

                    @Override
                    public void onComplete() {
                        listener.onFinish();
                    }
                });
    }

    @Override
    public void ManualNewReportInfo(String code, double weight, double volume, double length, double width, double height, OnNetRequestListener<ManualResult> listener) {
        //如果是异步操作就把返回的Disposable 取出 添加
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), new Gson().toJson(new ManualPamars(code, weight, volume, length, width, height)));
        Observable<ManualResult> observable = webRequest.ManualNewReportInfo(requestBody);
        listener.onStart();
        observable.compose(RxScheduler.<ManualResult>Obs_io_main())

                .subscribe(new Subscribe2Help<ManualResult>(mRxPresenter) {
                    @Override
                    public void onNext(ManualResult value) {
                        listener.onSuccess(value);
                    }

                    @Override
                    public void onError(Throwable throwable) {
                        super.onError(throwable);
                        listener.onFailure(throwable);
                    }

                    @Override
                    public void onComplete() {
                        listener.onFinish();
                    }
                });
    }
}